{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 安装依赖\n",
    "!pip install langchain langchain-openai\n",
    "\n",
    "###代码样例目录\n",
    "#https://github.com/langchain-ai/langchain/tree/master/docs/docs/modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "# 确认环境变量中已经配置OPENAI_API_KEY\n",
    "llm = ChatOpenAI()"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "AIMessage(content='Langsmith can be a valuable tool for testing in several ways:\\n\\n1. **Automated Testing**: Langsmith can be used to automate testing by generating test cases based on specified inputs and outputs. This can help in reducing manual testing efforts and ensuring comprehensive test coverage.\\n\\n2. **Test Data Generation**: Langsmith can generate realistic and diverse test data to simulate different scenarios and edge cases. This can help in validating the robustness and reliability of the software under test.\\n\\n3. **Performance Testing**: Langsmith can be used to simulate high loads and stress conditions to assess the performance of the software. This can help in identifying performance bottlenecks and optimizing the application for better scalability.\\n\\n4. **Regression Testing**: Langsmith can generate regression test cases to ensure that new changes or updates do not introduce any unintended side effects or break existing functionality.\\n\\n5. **Integration Testing**: Langsmith can generate test cases to validate the integration of different components or systems. This can help in ensuring that the integrated system functions as expected.\\n\\nOverall, Langsmith can streamline the testing process, increase test coverage, and improve the quality of software by providing automated and intelligent test case generation capabilities.', response_metadata={'token_usage': {'completion_tokens': 233, 'prompt_tokens': 27, 'total_tokens': 260}, 'model_name': 'gpt-3.5-turbo', 'system_fingerprint': 'fp_b28b39ffa8', 'finish_reason': 'stop', 'logprobs': None})"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\", \"You are world class technical documentation writer.\"),\n",
    "    (\"user\", \"{input}\")\n",
    "])\n",
    "chain = prompt | llm\n",
    "chain.invoke({\"input\": \"how can langsmith help with testing?\"})"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from langchain_core.output_parsers import StrOutputParser\n",
    "output_parser = StrOutputParser()\n",
    "chain = prompt | llm | output_parser\n",
    "chain.invoke({\"input\": \"how can langsmith help with testing?\"})"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
